package com.qing.demo.netty.client;

import cn.hutool.core.util.ObjUtil;
import com.qing.demo.netty.future.SyncWriteFuture;
import com.qing.demo.netty.future.SyncWriteMap;
import com.qing.demo.netty.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author caozhipeng
 * @date 2025/8/2
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        Response msg=(Response)obj;
        String responseId = msg.getResponseId();
        SyncWriteFuture future= (SyncWriteFuture) SyncWriteMap.syncKey.get(responseId);
        if(ObjUtil.isNotNull(future)){
            future.setResponse(msg);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
